﻿using System;
using System.Collections.Generic;
using System.Text;
using SyncCommon.DAL;
using System.Drawing;
using System.IO;
using System.Text.RegularExpressions;

namespace SyncCommon
{
    public abstract class SyncHouseBase : ISyncHouse
    {
        public event UpdateStatusHandler UpdateStatus;
        public event ValidationCodeHandler ValidationCode;
        private long websiteID;
        private int rowID;
        protected string UserName;
        protected string Password;
        public const string WebError = "网络错误，请稍后点击重发，如果仍然无法发送成功，则可能服务器正在维护";
        protected int errorImage = 0;
        public long WebSiteID
        {
            get
            {
                return websiteID;
            }
            set
            {
                websiteID = value;
            }
        }

        public int RowID
        {
            get
            {
                return rowID;
            }
            set
            {
                rowID = value;
            }
        }

        public SyncHouseBase(string username, string password)
        {
            UserName = username;
            Password = password;
        }

        public virtual bool CheckLogin()
        {
            return false;
        }

        public bool AddHouse(HouseInfo info, ref string targetID)
        {
            try
            {
                if (info.TradeType == TradeType.Sale)
                {
                    return AddSaleHouse(info, ref targetID);
                }
                else
                {
                    return AddRentHouse(info, ref targetID);
                }
            }
            catch (Exception exp)
            {
                LogCenter.Log("添加房源出错", exp);
                UpdateCurrentStatus(StandardProgress.Failed, "添加出现异常。异常;" + exp.Message);
            }
            return false;
        }

        public bool DeleteHouse(HouseInfo info, string targetID)
        {
            try
            {
                if (info.TradeType == TradeType.Sale)
                {
                    return DeleteSaleHouse(info, targetID);
                }
                else
                {
                    return DeleteRentHouse(info, targetID);
                }
            }
            catch (Exception exp)
            {
                LogCenter.Log("删除房源出错", exp);
                UpdateCurrentStatus(StandardProgress.Failed, "删除出现异常。异常;" + exp.Message);
            }
            return false;
        }

        public bool RefreshHouse(HouseInfo info, string targetID)
        {
            try
            {
                if (info.TradeType == TradeType.Sale)
                {
                    return RefreshSaleHouse(info, targetID);
                }
                else
                {
                    return RefreshRentHouse(info, targetID);
                }
            }
            catch (Exception exp)
            {
                LogCenter.Log("刷新房源出错", exp);
                UpdateCurrentStatus(StandardProgress.Failed, "刷新出现异常。异常;" + exp.Message);
            }
            return false;
        }


        public virtual bool AddSaleHouse(HouseInfo info, ref string targetID)
        {
            UpdateCurrentStatus(StandardProgress.Failed, "网站不支持出售房源添加");
            return false;
        }

        public virtual bool AddRentHouse(HouseInfo info, ref string targetID)
        {
            UpdateCurrentStatus(StandardProgress.Failed, "网站不支持出租房源添加");
            return false;
        }

        public virtual bool DeleteSaleHouse(HouseInfo info, string targetID)
        {
            UpdateCurrentStatus(StandardProgress.Failed, "网站不支持出售房源删除");
            return false;
        }

        public virtual bool DeleteRentHouse(HouseInfo info, string targetID)
        {
            UpdateCurrentStatus(StandardProgress.Failed, "网站不支持出租房源删除");

            return false;
        }

        public virtual bool RefreshSaleHouse(HouseInfo info, string targetID)
        {
            UpdateCurrentStatus(StandardProgress.Failed, "网站不支持出售房源刷新");

            return false;
        }
        public virtual bool RefreshRentHouse(HouseInfo info, string targetID)
        {
            UpdateCurrentStatus(StandardProgress.Failed, "网站不支持出租房源刷新");
            return false;
        }

        public void UpdateCurrentStatus(StandardProgress progress, string status, string target, string url)
        {
            if (UpdateStatus != null)
            {
                UpdateStatus(this, new StatusData() { Progress = progress, Status = status, TargetID = target, Url = url });
            }
        }
        public void UpdateCurrentStatus(StandardProgress progress, string status)
        {
            UpdateCurrentStatus(progress, status, "", "");
        }

        public void UpdateCurrentStatus(StandardProgress progress)
        {
            UpdateCurrentStatus(progress, "", "", "");
        }

        protected string GetValidationCode(Stream content)
        {
            if (ValidationCode != null)
            {
                return ValidationCode(Image.FromStream(content));
            }
            return "";
        }

        public virtual bool CommonCheck(HouseInfo infor, bool isSale, ref string errorInfor)
        {
            return
                CheckHefa(infor, isSale, ref errorInfor)
                && CheckEstate(infor, isSale, ref errorInfor)
                && CheckCity(infor, isSale, ref errorInfor)
                && CheckDistrict(infor, isSale, ref errorInfor)
                && CheckDiduan(infor, isSale, ref errorInfor)
                && CheckAddress(infor, isSale, ref errorInfor)
                && CheckTitle(infor, isSale, ref errorInfor)
                && CheckHuxing(infor, isSale, ref errorInfor)
                && CheckFloor(infor, isSale, ref errorInfor)
                && CheckArea(infor, isSale, ref errorInfor)
                && CheckAge(infor, isSale, ref errorInfor)
                && CheckDescription(infor, isSale, ref errorInfor)
                && CheckTelephone(infor, isSale, ref errorInfor)
                && CheckContact(infor, isSale, ref errorInfor)
                && CheckPrice(infor, isSale, ref errorInfor);
        }
        public virtual bool CheckHefa(HouseInfo infor, bool isSale, ref string errorInfor)
        {
            if (string.IsNullOrEmpty(infor.HefaCode))
            {
                errorInfor = "合发码不能为空";
                return false;
            }
            return true;
        }

        public virtual bool CheckEstate(HouseInfo infor, bool isSale, ref string errorInfor)
        {
            if (string.IsNullOrEmpty(infor.Estate))
            {
                errorInfor = "小区名不能为空";
                return false;
            }
            return true;
        }
        public virtual bool CheckCity(HouseInfo infor, bool isSale, ref string errorInfor)
        {
            if (string.IsNullOrEmpty(infor.City))
            {
                errorInfor = "城市不能为空";
                return false;
            }
            return true;
        }
        public virtual bool CheckDistrict(HouseInfo infor, bool isSale, ref string errorInfor)
        {
            if (string.IsNullOrEmpty(infor.LocalDistrict))
            {
                errorInfor = "区/县信息不能为空";
                return false;
            }
            return true;
        }
        public virtual bool CheckDiduan(HouseInfo infor, bool isSale, ref string errorInfor)
        {
            if (string.IsNullOrEmpty(infor.LocalDiduan))
            {
                errorInfor = "地段信息不能为空";
                return false;
            }
            return true;
        }

        public virtual bool CheckAddress(HouseInfo infor, bool isSale, ref string errorInfor)
        {
            if (string.IsNullOrEmpty(infor.LocalAddress))
            {
                errorInfor = "详细地址不能为空";
                return false;
            }
            return true;
        }
        public virtual bool CheckTitle(HouseInfo infor, bool isSale, ref string errorInfor)
        {
            if (string.IsNullOrEmpty(infor.Title))
            {
                errorInfor = "标题不能为空";
                return false;
            }
            if (infor.Title.Length < 6
                || infor.Title.Length > 25)
            {
                errorInfor = "标题必须6-25字，不能包括电话等信息";
                return false;
            }
            return true;
        }

        public virtual bool CheckHuxing(HouseInfo infor, bool isSale, ref string errorInfor)
        {
            if (string.IsNullOrEmpty(infor.Room)
                || string.IsNullOrEmpty(infor.Hall)
                || string.IsNullOrEmpty(infor.WashRoom))
            {
                errorInfor = "户型信息:室/厅/卫 不能为空";
                return false;
            }
            int room = 0;
            int hall = 0;
            int washroom = 0;
            if (!int.TryParse(infor.Room, out room)
                || !int.TryParse(infor.Hall, out hall)
                || !int.TryParse(infor.WashRoom, out washroom))
            {
                errorInfor = "户型：室/厅/卫 必须为数字。";
                return false;
            }

            if (room <= 0)
            {
                errorInfor = "户型: 室 必须为数字且大于0。";
                return false;
            }
            return true;
        }

        public virtual bool CheckFloor(HouseInfo infor, bool isSale, ref string errorInfor)
        {
            int currentFloor;
            int floors;
            if (!int.TryParse(infor.CurrentFloor, out currentFloor)
              || !int.TryParse(infor.Floors, out floors))
            {
                errorInfor = "楼层信息不能为空，且必须为有效数字。";
                return false;
            }

            if (currentFloor <= 0)
            {
                errorInfor = "当前楼层必须大于0";
                return false;
            }
            if (floors <= 0)
            {
                errorInfor = "总楼层必须大于0";
                return false;
            }
            return true;
        }

        public virtual bool CheckArea(HouseInfo infor, bool isSale, ref string errorInfor)
        {
            int area = 0;
            if (!int.TryParse(infor.Area, out area))
            {
                errorInfor = "面积不能为空，且必须为有效数字";
                return false;
            }
            if (area <= 0)
            {
                errorInfor = "面积必须大于0";
                return false;
            }
            return true;
        }
        public virtual bool CheckAge(HouseInfo infor, bool isSale, ref string errorInfor)
        {
            if (string.IsNullOrEmpty(infor.Age))
            {
                errorInfor = "建筑年代不能为空";
                return false;
            }
            int age = 0;
            if (!int.TryParse(infor.Age, out age))
            {
                errorInfor = "建筑年代必须是有效4位数字";
                return false;
            }
            return true;
        }


        public virtual bool CheckDescription(HouseInfo infor, bool isSale, ref string errorInfor)
        {
            if (string.IsNullOrEmpty(infor.Description))
            {
                errorInfor = "说明不能为空";
                return false;
            }

            if (infor.Description.Length < 10)
            {
                errorInfor = "说明必须多于10字";
                return false;
            }
            return true;
        }

        public virtual bool CheckTelephone(HouseInfo infor, bool isSale, ref string errorInfor)
        {
            string temp = @"((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)";
            Regex reg = new Regex(temp);

            if (!reg.IsMatch(infor.Mobile)
                || reg.Match(infor.Mobile).Value != infor.Mobile)
            {
                errorInfor = "联系方式格式错误";
                return false;
            }
            return true;
        }
        public virtual bool CheckContact(HouseInfo infor, bool isSale, ref string errorInfor)
        {
            if (string.IsNullOrEmpty(infor.Name))
            {
                errorInfor = "联系人不能为空";
                return false;
            }

            if (infor.Name.Length < 2
                || infor.Name.Length > 6)
            {
                errorInfor = "联系人必须为2-6字汉字或字母";
                return false;
            }
            return true;
        }

        public virtual bool CheckPrice(HouseInfo infor, bool isSale, ref string errorInfor)
        {
            if (isSale)
            {
                if (string.IsNullOrEmpty(infor.Price))
                {
                    errorInfor = "售价不能为空";
                    return false;
                }
            }
            else
            {
                if (string.IsNullOrEmpty(infor.MonthPrice))
                {
                    errorInfor = "租金不能为空";
                    return false;
                }
            }
            return true;
        }

        public string GetImageError(HouseInfo info)
        {
            if (info.Images != null
                && info.Images.Length > 0
                && errorImage > 0
                )
            {
                return string.Format("{0}张图片发布失败。", errorImage);
            }
            return "";
        }

        public virtual bool OpenEidt(WebBrowserOp op,HouseInfo info)
        {
            return false;
        }
    }
}
